package modules.enlist
{
	import com.adobe.utils.DictionaryUtil;
	
	import core.Language;
	import core.StageProxy;
	import core.manager.CostManager;
	import core.model.PlayerModel;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	
	import framework.view.components.container.IRenderProxy;
	
	import values.avatar.AvatarTypeValue;
	import values.avatar.AvatarTypeValueList;
	import values.avatar.AvatarValue;
	import values.enlist.EnlistTemplateValue;
	import values.enlist.EnlistTemplateValueList;
	import values.events.AvatarForceEvent;
	import values.events.ItemValueEvent;
	import values.item.ItemTemplateKeyType;
	
	import view.component.window.ClassifiedPaginationWindowProxy;
	import view.component.window.event.NavigationChangeEvent;
	
	public class EnlistUIProxy extends ClassifiedPaginationWindowProxy
	{
		public function EnlistUIProxy()
		{
			super();
		}
		
		[Inject]
		public var playerModel:PlayerModel;
		
		override protected function get navItemRenderClass():Class
		{
			return EnlistNavRenderProxy;
		}
		
		override protected function get repeaterItemRenderClass():Class
		{
			return EnlistAvatarRenderProxy;
		}
		
		override protected function get navData():*
		{
			var firstAtv:AvatarTypeValue = new AvatarTypeValue();
			firstAtv.baseNameKey = "@all";
			firstAtv.key = "all_for_all";
			var arr:Array = AvatarTypeValueList.arrList;
			arr.unshift(firstAtv);
			return arr;
		}
		
		override public function parse(target:MovieClip):void
		{
			super.parse(target);
			skin.titleTF.text = Language.getText("@building_2_label");
			playerModel.myForce.addEventListener(AvatarForceEvent.HIRE_HERO , onHireHero);
			initShow();
			onResourceChange();            
			playerModel.getNumbericItem(ItemTemplateKeyType.COIN).addEventListener(ItemValueEvent.NUM_CHANGED , onResourceChange);
			playerModel.getNumbericItem(ItemTemplateKeyType.CASH).addEventListener(ItemValueEvent.NUM_CHANGED , onResourceChange);
		}
		
		public function initShow():void
		{
			for each (var render:IRenderProxy in navRepeater.itemProxys)
			{
				if (render.data && AvatarTypeValue(render.data).key == "all_for_all")
				{
					render.selected = true;
					break;
				}
			}
		}
		
		override protected function get currentTypeDataLength():*		
		{
			return currentTypeData.length;
		}
		
		
		private function onHireHero(evt:AvatarForceEvent):void
		{
            var avatar:AvatarValue = evt.params.avatar;
            var template:EnlistTemplateValue = EnlistTemplateValueList.getEnlistByAvatarKey(avatar.key);
            CostManager.cost(template.cost);
            StageProxy.unlock();
			refresh();
		}
		
		private function onResourceChange(evt:Event = null):void
		{
			skin["coinTF"].text = playerModel.getItemNum(ItemTemplateKeyType.COIN);
			skin["cashTF"].text = playerModel.getItemNum(ItemTemplateKeyType.CASH);
			skin["reputationTF"].text = playerModel.myPlayerAvatar.reputation.toString();
		}
		
		override public function dispose():void
		{
			playerModel.getNumbericItem(ItemTemplateKeyType.COIN).removeEventListener(ItemValueEvent.NUM_CHANGED , onResourceChange);
			playerModel.getNumbericItem(ItemTemplateKeyType.CASH).removeEventListener(ItemValueEvent.NUM_CHANGED , onResourceChange);
			playerModel.myForce.removeEventListener(AvatarForceEvent.HIRE_HERO , onHireHero);
			super.dispose();
		}
		
		override protected function navigationChangeHandler(event:NavigationChangeEvent):void
		{
			var vo:AvatarTypeValue = AvatarTypeValue(event.param);
			for each (var render:IRenderProxy in navRepeater.itemProxys)
			{
				if (render.data && AvatarTypeValue(render.data).key != vo.key)
				{
					render.selected = false;
				}
			}
			currentType = vo.key;
			updateList(currentTypeData, currentTypeDataLength, 0);
		}
		
		override protected function initData():void
		{
			var list:Array = navData;
			for each (var vo:AvatarTypeValue in list)
			{
				var arr:Array = [];
				var addOne:Boolean = false;
				var av:EnlistTemplateValue;
				if(vo.key != "all_for_all")
				{
					for each(av in EnlistTemplateValueList.getEnlistByType(vo.key))
					{
						if(!playerModel.myForce.getAvatarByKey(av.avatarKey) && ( av.reputation <= playerModel.myPlayerAvatar.reputation || !addOne ))
						{
							if(av.reputation > playerModel.myPlayerAvatar.reputation)
							{
								addOne = true;
							}
							arr.push(av);
						}
					}
				}
				else
				{
					for each(av in EnlistTemplateValueList.enlistList)
					{
						if(!playerModel.myForce.getAvatarByKey(av.avatarKey) && ( av.reputation <= playerModel.myPlayerAvatar.reputation || !addOne ))
						{
							if(av.reputation > playerModel.myPlayerAvatar.reputation)
							{
								addOne = true;
							}
							arr.push(av);
						}
					}
				}
				arr.sortOn("reputation" , Array.NUMERIC);
				addTypeData(vo.key, arr);
			}
		}
		
		override public function show(lock:Boolean=true, position:Point=null, ...args):void
		{
			super.show.apply(this, [ lock, position ].concat(args));
		}
		
		public function refresh():void
		{
			initData();
			var pageCount:int = Math.ceil(currentTypeDataLength / preCount);
			
			var page:int = currentPage;
			if (pageCount - 1 < currentPage)
			{
				page = pageCount - 1;
				if (page < 0)
				{
					page = 0;
				}
			}
			updateList(currentTypeData, currentTypeDataLength, page);
		}
	}
}